package com.open.capacity.reimburse.controller;

import java.util.Map;

import com.open.capacity.commons.Result;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.reimburse.entity.SysEmail;
import com.open.capacity.reimburse.service.SysEmailService;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * 邮件表
 *
 * @author
 * @email
 * @date 2021-06-25 15:47:01
 */
@RestController
@RequestMapping("emails")
@Api(tags = "邮件表")
public class SysEmailController {

    @Autowired
    private SysEmailService sysEmailService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('sys_email:sysemail:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = sysEmailService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('sys_email:sysemail:list')")
    public Result findById(String id) {
        return Result.succeed(sysEmailService.findById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('sys_email:sysemail:save')")
    public Result save(@RequestBody @Valid SysEmail sysEmail, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            sysEmailService.save(sysEmail);
        } else {
            sysEmail.setId(keyValue);
            sysEmailService.update(sysEmail);
        }
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('sys_email:sysemail:delete')")
    public Result delete(@PathVariable String id, String type) {
        sysEmailService.delete(id, type);
        return Result.succeed("操作成功");
    }

    /**
     * 彻底删除
     */
    @DeleteMapping("/thoroughRemove/{id}")
    @PreAuthorize("hasAnyAuthority('sys_email:sysemail:thoroughRemove')")
    public Result thoroughRemove(@PathVariable String id, String type) {
        sysEmailService.thoroughRemove(id, type);
        return Result.succeed("操作成功");
    }

    /**
     * 删除草稿
     */
    @DeleteMapping("/deleteDraft/{id}")
    @PreAuthorize("hasAnyAuthority('sys_email:sysemail:deleteDraft')")
    public Result deleteDraft(@PathVariable String id, String type) {
        sysEmailService.deleteDraft(id, type);
        return Result.succeed("操作成功");
    }

    /**
     * 还原
     */
    @RequestMapping("/restore/{id}")
    @PreAuthorize("hasAnyAuthority('sys_email:sysemail:restore')")
    public Result restore(@PathVariable String id, String type) {
        sysEmailService.restore(id, type);
        return Result.succeed("还原成功");
    }

}
